package com.lfg.learn.weixin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.time.DateUtils;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class Demo {


    public static final int PAGE_SIZE = 900;
    public static String token = "56_gI5KU9FKGRGIoekmIrsIb7H3xHDI1wnTwTcyXD7tb6oI0w2W6CGkd8fqplyOO5R9jHGAL6LTWmRFFbtV59XBxYx5_BWHKsOTSEwEtyDwxZYBuJB88Z2B9hLadyrhZA2d7Du1fa__o4mAO3csCJWgAIAIPC";
    public static String searchUrl = "https://api.weixin.qq.com/wxa/search/wxaapi_submitpages?access_token="+ token;

    public static void main(String[] args) throws Exception {

        HashMap<String, Object> map = new HashMap<>();
        map.put("", "");
        ConcurrentHashMap<String, Object> map2 = new ConcurrentHashMap<>();
        map2.put("", "");

        Executor executor = Executors.newSingleThreadExecutor();
//        executor.execute();

        sendData();
//        deleteData();
    }

    private static void deleteData() throws UnsupportedEncodingException, ParseException {
        List<InfoEntity> infos = new ArrayList<>();
        String str = "000880170,000879621,000879622,000879209,000879208,000879207,000879378,000879073,000878707,000878699,000878458,000878253,000877879,000877449,000877230,000876526,000875608,000875253,000874845,000874367,000873820,000874260,000874166,000873527,000873913,000873862,000873442,000873259,000873316,000873326,000872792,000872405,000872250,000871999,000871802,000871763,000870750,000870418,000869333,000869700,000868604,000868044,000864982,000867405,000867331,000866619,000866046,000865665,000865616,000865378,000864925,000864445,000864417,000863536,000863156,000862617,000861851,000861665,000860851,000860850,000861153,000860837,000860722,000860628,000860303,000854969,000859957,000859432,000858961,000858668,000858234,000857519,000856462,000856461,000856186,000856149,000856090,000856031,000856023,000856013,000855820,000855802,000855788,000855783,000855782,000855776,000855774,000855654,000855692,000855641,000855312,000855277,000855231,000855159,000855151,000855705,000854963,000854835,000853529,000854829";

        String[] s = str.split(",");

        for (int i = 0; i < s.length; i++) {
            InfoEntity info = new InfoEntity();
            info.setInfoID(s[i]);
            info.setFocusPhoto("https://cdn.sdb.com.cn/app_upload/brop-mop/news/default/focusPhoto/00000006.jpg");
            info.setInfoTitle("2022胡润");
            info.setImgUrl("https://ebank.pingan.com.cn/info/zixun/detail.shtml?id=000875608&outerSource=zxpt_2203_005376");
            info.setCreatedDate("2022-03-18 17:48:00");
            info.setStatus(3);

            info.setInfoSummary("胡润研究院发布《2022胡润全球富豪榜》");
            infos.add(info);
        }
        sendWeiXin(infos);
    }

    public static void sendData() throws Exception {
        String url = "https://rsb.pingan.com.cn/brop/mop/adms/cust/admssf/information/list?labelId=C10&pageNo=1&pageSize=100";

        String ret = HttpClientUtils.get(url, null, null);
        JSONObject json = JSONObject.parseObject(ret);
        JSONArray infoList = json.getJSONArray("infoList");
        List<InfoEntity> infos = new ArrayList<>();
        List<String> ids = new ArrayList<>();


        for (int i = 0; i < infoList.size(); i++) {
            try {
                JSONObject data = infoList.getJSONObject(i);
                InfoEntity info = new InfoEntity();
                info.setInfoID(data.getString("id"));
                info.setFocusPhoto(data.getString("focusPhoto"));
                info.setInfoTitle(data.getString("title"));
                info.setImgUrl(data.getString("pcUrl"));
                info.setCreatedDate(data.getString("publishTime"));
                String url2 = "https://rsb.pingan.com.cn/brop/mop/adms/cust/admssf/uc/information/detail?id=" + info.getInfoID();
                String content = HttpClientUtils.get(url2, null, null);
                JSONObject json2 = JSON.parseObject(content).getJSONObject("info");
                info.setInfoText(json2.getString("contentHtml"));
                if (info.getInfoText() == null || info.getInfoText().length() == 0) {
                    info.setStatus(3);
                } else {
                    info.setStatus(1);
                }
                info.setInfoSummary(data.getString("digest"));
                infos.add(info);
                ids.add(info.getInfoID());
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.print(JSON.toJSONString(ids));
        }
        sendWeiXin(infos);
    }

    public static void sendWeiXin(List<InfoEntity> infos) throws ParseException, UnsupportedEncodingException {



        int pageNo = infos.size() / PAGE_SIZE;
        if (infos.size() % PAGE_SIZE != 0) {
            pageNo++;
        }
        for(int i=0;i< pageNo;i++) {
            try {
                List<Map<String, Object>> mapList = new ArrayList<>();
                for (int j = i * PAGE_SIZE; j < (i + 1) * PAGE_SIZE  && j<infos.size(); j++) {
                    List<Map> list = new ArrayList<>();
                    InfoEntity info = infos.get(j);
                    Map<String, Object> data = new HashMap<>();
                    list.add(data);
                    data.put("@type", "wxsearch_testcpdata");
                    data.put("update", info.getStatus());
                    data.put("content_id", info.getInfoID());

                    data.put("page_type", 2);
                    data.put("category_id", 1);
                    data.put("title", info.getInfoTitle());

                    data.put("cover_img_url", info.getImgUrl());
                    Date date = DateUtils.parseDate(info.getCreatedDate(), "yyyy-MM-dd HH:mm:ss");
                    data.put("time_publish", ((Long) (date.getTime() / 1000)).intValue());
                    data.put("time_modify", System.currentTimeMillis() / 1000);
                    data.put("mainbody", info.getInfoText());
                    data.put("author_name", "");
                    data.put("abstract", new String[]{info.getInfoSummary()});


                    Map<String, Object> paramMap = new HashMap<>();
                    paramMap.put("grant_type", "client_credential");
                    paramMap.put("appid", "wx17511f342beb6229");
                    paramMap.put("secret", "88a824facb11534f56c89dc545a5c00e");
                    paramMap.put("query", "infoId=" + info.getInfoID());
                    paramMap.put("path", "pages/packageD/infomation/main");
                    paramMap.put("data_list", list);

                    mapList.add(paramMap);
                }
                Map<String, Object> pages = new HashMap<>();
                pages.put("pages", mapList);

                String ret = HttpClientUtils.post(searchUrl, null, null, JSON.toJSONString(pages));
                System.out.print("ret=" + ret);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
